#!/bin/bash

#
# Simple script to list the contents of remote FTP server
# directory and redirect into output file (for subsequent Java
# parsing.  See HMCRestoreTasklet.
#
function doFTP {
    SERVER=$1
    USER=$2
    PASSWORD=$3

    /usr/kerberos/bin/ftp -n -u $SERVER <<EOF 2>&1 > /var/hsc/log/ftp.info
    user $USER $PASSWORD
ls
quit
EOF
    
   # save rc for return
   grep -i "failed" /var/hsc/log/ftp.info
   if [ $? -ne 0 ]; then
       # try looking for this
       grep -i "unknown" /var/hsc/log/ftp.info
       if [ $? -ne 0 ]; then
           # try looking for this
           grep -i "cannot" /var/hsc/log/ftp.info
           if [ $? -ne 0 ]; then
               funcRC=0
           else
               funcRC=23
           fi
       else
           funcRC=22
       fi
   else
       funcRC=21
   fi
   
   return $funcRC
}

#--------------------Main Program--------------------------             
doFTP $2 $3 $4
rc=$?
    
exit $rc
